//
//  YDYOauthViewController.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/20.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYOauthViewController.h"
#import "YDYWelcomeViewController.h"
#import "YDYNetWorkTool.h"
#import "YDYUserAccount.h"
#import <SVProgressHUD.h>
@interface YDYOauthViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *OauthWebView;

@end
static YDYUserAccount *account ;
@implementation YDYOauthViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self startOauth];
}

-(void)startOauth{
    
    NSString *urlString =[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",ydy_client_id,ydy_redirect_uri] ;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    [self.OauthWebView loadRequest:request];
}

#pragma mark -
#pragma mark -WebViewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD showWithStatus:@"正在加载"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"加载错误,代码:%ld",(long)error.code]];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if ([request.URL.absoluteString isEqualToString:@"https://api.weibo.com/oauth2/authorize?client_id=2426708658&redirect_uri=http://www.baidu.com"]) {
        return YES;
    }
    if ([request.URL.absoluteString isEqualToString:@"https://api.weibo.com/oauth2/authorize"]) {
        return YES;
    }
    
    if ([request.URL.absoluteString hasPrefix:@"http://www.baidu.com"]) {
        //code=508128e04fb8e074d4ed010ac11db206 index = 5
        NSString *code = [request.URL.query substringFromIndex:5];
        [self getAccessToken:code];
        
    }
    
    return NO;
}
//获取用户access token
-(void)getAccessToken:(NSString *)code{
    if (code == nil)return;
    
    NSString *url = @"oauth2/access_token";
    NSDictionary *parameters =@{@"client_id":ydy_client_id,@"client_secret":ydy_client_secret,@"grant_type":@"authorization_code",@"code":code,@"redirect_uri":ydy_redirect_uri};
    [[YDYNetWorkTool sharedTool]POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        YDYUserAccount *account = [[YDYUserAccount alloc]initWithDictonary:responseObject];
        //保存
        [account loadUserInfoFinished:^(YDYUserAccount *account, NSError *error) {
            [account saveUserAccount];
            //跳转到欢迎界面
            [[NSNotificationCenter defaultCenter]postNotificationName:YDYChangeRootViewControllerNotification object:self];
//            [self jumpToWelcomeController];
        }];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YDYLog(@"task:%@,response = %@",task,error);
    }];
    
    
}
-(void)jumpToWelcomeController{
    [UIApplication sharedApplication].keyWindow.rootViewController = [UIStoryboard storyboardWithName:NSStringFromClass([YDYWelcomeViewController class]) bundle:nil].instantiateInitialViewController;
}
@end
